<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

?>
<script type="text/javascript">
//<![CDATA[
<?php
    /** @var $myBlock Mage_Api2_Block_Adminhtml_Roles_Tab_Users */
    $myBlock = $this->getLayout()->getBlock('adminhtml.role.edit.tab.users');
    $name = $myBlock->getJsObjectName();
?>
    <?php if (is_object($myBlock) && $name && !$myBlock->isHidden()): ?>
    var checkBoxes = $H(<?php echo $myBlock->getUsers(true) ?>);
    var warning = false;
    if (checkBoxes.size() > 0) {
        warning = true;
    }
    $('in_role_users').value = checkBoxes.toQueryString();

    function registerUserRole(grid, element, checked)
    {
        if (checked) {
            checkBoxes.set(element.value, 0);
        } else {
            checkBoxes.unset(element.value);
        }
        $('in_role_users').value = checkBoxes.toQueryString();
        grid.reloadParams = {'in_role_users[]':checkBoxes.keys()};
    }

    function roleUsersRowClick(grid, event)
    {
        var trElement = Event.findElement(event, 'tr');
        var isInput   = Event.element(event).tagName == 'INPUT';
        if (trElement) {
            var checkbox = Element.getElementsBySelector(trElement, 'input');
            if(checkbox[0]){
                var checked = isInput ? checkbox[0].checked : !checkbox[0].checked;
                if (checked) {
                    checkbox[0].checked = true;
                    if ( confirm("<?php echo Mage::helper('core')->jsQuoteEscape($this->__('Warning!\r\nThis action will remove this user from already assigned role\r\nAre you sure?')) ?>") ) {
                        checkBoxes.set(checkbox[0].value, checked);
                        varienElementMethods.setHasChanges(checkbox[0]);
                    } else {
                        checkbox[0].checked = false;
                    }
                    warning = false;
                } else {
                    checkBoxes.unset(checkbox[0].value);
                }

                $('in_role_users').value = checkBoxes.toQueryString();
                grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
            }
        }
    }

    function roleUsersRowInit(grid, row)
    {
        var checkbox = $(row).getElementsByClassName('checkbox')[0];
        if (checkbox) {
            if (checkbox.checked) {
                checkBoxes.set(checkbox.value, checkbox.checked);
            } else {
                checkBoxes.unset(checkbox.value);
            }

            $('in_role_users').value = checkBoxes.toQueryString();
            grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
        }
    }

    function myhandler(o)
    {
        if (checkBoxes.size() > 0) {
            if (!confirm("<?php echo Mage::helper('core')->jsQuoteEscape($this->__('Warning!\r\nThis action will remove those users from already assigned roles\r\nAre you sure?')) ?>") ) {
                o.checked = !o.checked;
            } else {
                checkBoxes.each(function(el){
                    el.checked = o.checked;
                });
                return false;
            }
            warning = false;
        }

        $('in_role_users').value = checkBoxes.toQueryString();
        //grid.reloadParams = {'in_role_user[]':checkBoxes.keys()};
    }

        <?php echo $name ?>.rowClickCallback = roleUsersRowClick;
        <?php echo $name ?>.initRowCallback = roleUsersRowInit;
        <?php echo $name ?>.checkboxCheckCallback = registerUserRole;
        <?php echo $name ?>.checkCheckboxes = myhandler;
        <?php echo $name ?>.rows.each(function(row){roleUsersRowInit(<?php echo $name ?>, row)});
    $('in_role_users_old').value = $('in_role_users').value;
        <?php endif; ?>
//]]>
</script>
